package graph;

import java.util.HashSet;
import java.util.Set;

/**
 * @author Alex
 * @param <V>
 * @param <W>
 *
 */
public class Subgraph<V, W> extends UndirectedGraph<V, W> implements Graph<V,W> {
	
	private UndirectedGraph<V, W> baseGraph;
	
	public Subgraph(UndirectedGraph<V, W> baseGraph, Set<Vertex<V>> vertexSubset) {
		super(vertexSubset, new HashSet<Edge<V,W>>());
		for(Vertex<V> v : this.getVertices()) {
			for(Edge<V,W> e : v.getEdges()) {
				if(this.getVertices().contains(e.getV1()) && 
						this.getVertices().contains(e.getV2())) {
					this.addEdge(e);
				}
			}
		}
		this.baseGraph = baseGraph;
	}
	
	public Subgraph(UndirectedGraph<V, W> base, String[] vertexNames) {
		super(new HashSet<Vertex<V>>(), new HashSet<Edge<V,W>>());
		for(String s : vertexNames) {
			Vertex<V> vertex = base.getVertex(s);
			if(vertex != null)
				this.addVertex(vertex);
		}
		for(Vertex<V> v : this.getVertices()) {
			for(Edge<V,W> e : v.getEdges()) {
				if(this.getVertices().contains(e.getV1()) && 
						this.getVertices().contains(e.getV2())) {
					this.addEdge(e);
				}
			}
		}
		this.baseGraph = base;
	}

	/**
	 * @return the baseGraph
	 */
	public UndirectedGraph<V, W> getBaseGraph() {
		return baseGraph;
	}

	/**
	 * @param baseGraph the baseGraph to set
	 */
	public void setBaseGraph(UndirectedGraph<V, W> baseGraph) {
		this.baseGraph = baseGraph;
	}
}
